package com.zc.gateway.api;

import com.zc.common.annotations.RouteHandler;
import com.zc.common.annotations.RouteMapping;
import com.zc.common.annotations.RouteMethod;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Name: RouterController
 * @Author: Neil.Zhou
 * @Version: V1.00
 * @CreateDate: 2016/08/07
 * @Description: 路由管理
 */
@RouteHandler
public class RouterController{
    private Logger logger = LoggerFactory.getLogger(RouterController.class);

    /**
     * @Author:Neil.Zhou
     * @CreateDate: 2016/06/14
     * @Version:V1.00
     * @Description: 登录
     */
    @RouteMapping(value = "/test1", method = RouteMethod.GET)
    public Handler<RoutingContext> getRouters() {
        return ctx -> {
            JsonObject param =new JsonObject();
            param.put("key", "test1");
            ctx.vertx().eventBus().<JsonObject>send("com.zc.gateway.service.RouterService",param,new DeliveryOptions().addHeader("method","doGetServiceTest"), resultBody -> {
                if (resultBody.failed()) {
                    logger.error("Fail for the process!");
                    ctx.fail(resultBody.cause());
                    return;
                }
                JsonObject result = resultBody.result().body();
                if (result == null) {
                    logger.error("Fail by result is null");
                    ctx.fail(500);
                    return;
                }
                logger.debug("-- the result==---" + result);
                ctx.response().setStatusCode(200);
                ctx.response().end(result.encode());
            });
        };
    }
}


